package com.bootx.dao.impl;

import com.bootx.dao.AiRoleChatMessageDao;
import com.bootx.entity.AiRole;
import com.bootx.entity.AiRoleChatMessage;
import com.bootx.entity.Member;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;

/**
 * @author black
 */
@Repository
public class AiRoleChatMessageDaoImpl extends BaseDaoImpl<AiRoleChatMessage,Long> implements AiRoleChatMessageDao {
    @Override
    public List<AiRoleChatMessage> findList(Member member, AiRole aiRole) {
        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<AiRoleChatMessage> criteriaQuery = criteriaBuilder.createQuery(AiRoleChatMessage.class);
        Root<AiRoleChatMessage> root = criteriaQuery.from(AiRoleChatMessage.class);
        criteriaQuery.select(root);
        Predicate restrictions = criteriaBuilder.conjunction();
        if (aiRole==null || member==null){
            return Collections.emptyList();
        }
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.join("aiRole"), aiRole));
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.join("member"), member));
        criteriaQuery.where(restrictions);
        criteriaQuery.orderBy(criteriaBuilder.asc(root.get("createDate")));
        return super.findList(criteriaQuery, null, null, null, null);
    }
}
